package com.bw.a2207a.view

import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity
import com.bw.a2207a.databinding.ActivityMainBinding
import com.bw.a2207a.viewmodel.RegisterViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener

@Route(path = "/main/MainActivity")
class MainActivity : BaseActivity<ActivityMainBinding, RegisterViewModel>(R.layout.activity_main) {

    @JvmField
    @Autowired
    var key: Int = 0


    @Autowired(name="userName")
    lateinit var userNameddd: String

    @Autowired
    lateinit var userName1: String


    //声明宿主容器NavHostFragment
    lateinit var navHostFragment: NavHostFragment
    private val tabArr = mutableListOf<String>("首页", "分类", "购物车", "我的")
    private val tabResArr = mutableListOf<Int>(
        R.drawable.ic_home_24,
        R.drawable.ic_type_24,
        R.drawable.ic_shopping_cart_24,
        R.drawable.ic_mine_24
    )

    override fun initData() {
    }

    override fun initView() {
        //使用自定义的扩展函数，弹出Toast
//        this.showToastMsg("MainActivity")
        //Arouter 注入代码
        ARouter.getInstance().inject(this)

        intent.getIntExtra("key", 0)

        //获取宿主容器
        navHostFragment = supportFragmentManager.findFragmentById(R.id.main_host) as NavHostFragment
        val navController = navHostFragment.navController


        tabArr.forEachIndexed { index, s ->
            binding.mainTab.addTab(
                binding.mainTab.newTab().setIcon(tabResArr[index]).setText(s)
            )
        }

//        binding.mainTab.addTab(
//            binding.mainTab.newTab().setIcon(R.drawable.ic_home_24).setText("首页")
//        )
//        binding.mainTab.addTab(
//            binding.mainTab.newTab().setIcon(R.drawable.ic_type_24).setText("分类")
//        )
//        binding.mainTab.addTab(
//            binding.mainTab.newTab().setIcon(R.drawable.ic_shopping_cart_24).setText("购物车")
//        )
//        binding.mainTab.addTab(
//            binding.mainTab.newTab().setIcon(R.drawable.ic_mine_24).setText("我的")
//        )

        //处理mainTab的点击事件，根据点击下标，跳转对应页面
        binding.mainTab.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when (tab?.position) {
                    0 -> {
                        //导航到具体页面
                        navController.navigate(R.id.homeFragment)
                    }

                    1 -> {
                        navController.navigate(R.id.typeFragment)
                    }

                    2 -> {
                        navController.navigate(R.id.cartFragment)
                    }

                    3 -> {
                        navController.navigate(R.id.mineFragment)
                    }
                }

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })


//        binding.mainTab.addOnTabSelectedListener(object : OnTabSelectedListener {
//            override fun onTabSelected(tab: TabLayout.Tab?) {
//                when (tab?.position) {
//                    0 -> navController.navigate(R.id.homeFragment)
//                    1 -> navController.navigate(R.id.typeFragment)
//                    2 -> navController.navigate(R.id.cartFragment)
//                    3 -> navController.navigate(R.id.mineFragment)
//                }
//            }
//
//            override fun onTabUnselected(tab: TabLayout.Tab?) {
//            }
//
//            override fun onTabReselected(tab: TabLayout.Tab?) {
//            }
//
//        })

    }
}